package me.lc.superDown.layer;

import me.lc.superDown.R;
import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.util.AudioHelper;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 暂停界面
 * 
 * @author Administrator
 * 
 */
public class PauseLayer extends Layer {
	Sprite bg;
	Button btn_resume;
	Button btn_retry;
	Button btn_return;

	public PauseLayer() {
		bg = Sprite.make(GameSystem.tex_pauseBg);
		bg.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2);
		addChild(bg);

		float startY = GameSystem.Screen_Height / 2
				+ GameSystem.tex_pauseBg.getHeight() / 2;

		btn_resume = Button.make(R.drawable.button_resume,
				R.drawable.button_resume_selected, this,
				"onResumeButtonClicked");
		btn_resume
				.setPosition(GameSystem.Screen_Width / 2, getNewY(startY, 78));
		addChild(btn_resume);

		btn_retry = Button.make(R.drawable.button_tryagain,
				R.drawable.button_tryagain_selected, this,
				"onRetryButtonClicked");
		btn_retry
				.setPosition(GameSystem.Screen_Width / 2, getNewY(startY, 125));
		addChild(btn_retry);

		btn_return = Button.make(R.drawable.button_return,
				R.drawable.button_return_selected, this,
				"onReturnButtonClicked");
		btn_return.setPosition(GameSystem.Screen_Width / 2,
				getNewY(startY, 172));
		addChild(btn_return);
	}

	private float getNewY(float startY, float y) {
		float bgHeight = ResolutionIndependent.resolveDp(186);
		return startY - (y / 186) * bgHeight;
	}

	public void onResumeButtonClicked() {
		AudioHelper.click();
		GameManager.instance.isPaused = false;
		this.setVisible(false);
		GameScene.make().layer.btn_pause.setVisible(true);
	}

	public void onRetryButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				GameManager.instance.startNewGame(GameScene.make().layer);
			}
		});
	}

	public void onReturnButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				GameManager.instance.startMainMenu(GameScene.make().layer);
			}
		});
	}
}
